package com.springboot.common.util;

import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * 时间、日期工具类
 */
public class DateUtils {

    public static Date now(){
        return from(LocalDateTime.now());
    }

    public static Date from(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }


    public static Date addSeconds(long sec) {
        return from(LocalDateTime.now().plusSeconds(sec));
    }

    public static long diffDate(Date d1, Date d2){
        LocalDate localDate1 = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(d1));
        LocalDate localDate2 = LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(d2));

        return localDate2.until(localDate1, ChronoUnit.DAYS);
    }


    public static void main(String[] args) {
        System.out.println(now());
        System.out.println(diffDate(now(), from(LocalDateTime.of(2019,3,1,0,0,0))));

//        System.out.println(diffDate());
    }
}
